<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Session;
use think\Db;

class Message extends Base
{

    /**
     * @param Request $request
     * @return \think\response\Json
     * 留言列表
     */
    public function index(Request $request)
    {
        $limit           = $request->param('limit') ?: 10;
        $page            = $request->param('page') ?: 1;
        $start_time      = $request->param('start_time') ?: '';
        $end_time        = $request->param('end_time') ?: '';
        $keyword         = $request->param('keyword') ?: '';
        $type            = $request->param('type');//1新留言  2意向客户 3已处理
        $message_form    = $request->param('message_form') ?: 1;//留言类型,1个性定制留言,2设计服务留言
        $city_select     = $request->param('city') ?: '';
        $province_select = $request->param('province') ?: '';
        $where           = [];

        if ($start_time && $end_time) {
            $time1                = strtotime($start_time);
            $time2                = strtotime($end_time);
            $where['create_time'] = ['between', "$time1,$time2"];
        }

        if ($keyword) {
            $where['content|name|tel'] = ['like', "%{$keyword}%"];
        }

        if ($type) {
            $where['type'] = $type;
        }

        if ($message_form) {
            $where['message_form'] = $message_form;
        }

        if ($city_select) {
            $where['ad_user_info.city'] = $city_select;
        }

        if ($province_select) {
            $where['ad_user_info.province'] = $province_select;
        }

        $message_list = Db::table('ad_message')->where($where)
            ->join('ad_user_info', 'ad_user_info.uid = ad_message.uid', 'LEFT')
            ->field([
                'ad_message.content',
                'ad_message.name',
                'ad_message.tel',
                'ad_message.id',
                'ad_message.message_form',
                'ad_message.type',
                'ad_message.create_time',
                'ad_user_info.job_two',
                'ad_user_info.province',
                'ad_user_info.city',
                'ad_user_info.uid',
            ])
            ->order(['create_time' => 'desc'])->paginate($limit,
                false,
                [
                    'page' => $page,
                    'query' => [
                        'keyword' => $keyword,
                        'start_time' => $start_time,
                        'end_time' => $end_time,
                        'type' => $type
                    ]
                ])->each(function ($item, $key) {
                $item['province_name'] = Db::table('ad_provinces')->where(['provinceid' => $item['province']])->value('province');
                $item['city_name']     = Db::table('ad_cities')->where(['cityid' => $item['city']])->value('city');
                $item['trade_name']    = Db::table('ad_industry')->where(['id' => $item['job_two']])->value('name');
                $item['type_name']     = getTypeName($item['type']);
                return $item;
            });

        //省份搜索数据
        $province = Db::table('ad_provinces')->select();
        $city     = Db::table('ad_cities')->select();
        return view('index',
            compact('message_list', 'province', 'city', 'type', 'message_form', 'start_time', 'end_time', 'city_select',
                'province_select'));
    }

    /**
     * @param Request $request
     * @return \think\response\View
     */
    public function info(Request $request)
    {
        $id                         = $request->param('id');
        $find_data                  = Db::table('ad_message')->where(['id' => $id])->find();
        $user_info                  = Db::table('ad_user_info')->where(['uid' => $find_data['uid']])->field([
            'job_two',
            'province',
            'city'
        ])->find();
        $find_data['province_name'] = Db::table('ad_provinces')->where(['provinceid' => $user_info['province']])->value('province');
        $find_data['city_name']     = Db::table('ad_cities')->where(['cityid' => $user_info['city']])->value('city');
        $find_data['trade_name']    = Db::table('ad_industry')->where(['id' => $user_info['job_two']])->value('name');

        return view('info', compact('find_data'));

    }

    /**
     * @param Request $request
     *
     */
    public function update(Request $request)
    {
        $id           = $request->param('id');
        $data['note'] = $request->param('note');
        $data['type'] = $request->param('type');
        if (Db::table('ad_message')->where(['id' => $id])->update($data)) {
            $this->success('提交成功');
        }
        $this->success('提交失败');

    }
}